package com.bbm.communitysystem.controller.chat;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.ChatPage;
import com.bbm.communitysystem.domain.dto.chat.ChatPageDto;
import com.bbm.communitysystem.domain.vo.ChatPageVo;
import com.bbm.communitysystem.service.impl.ChatPageServiceImpl;
import com.bbm.communitysystem.tools.DemoDate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-01
 */
@Tag(name = "AI聊天页面")
@RestController
@RequestMapping("/chat-page")
@RequiredArgsConstructor
public class ChatPageController {

    private final ChatPageServiceImpl chatPageService;

    @Operation(summary = "新增AI聊天页面")
    @PostMapping("/")
    public R addChatPage(@RequestBody ChatPageDto chatPageDto) {

        ChatPage chatPage = BeanUtil.copyProperties(chatPageDto, ChatPage.class);
        chatPage.setStatus(1);
        chatPage.setPageName("new Page");
        chatPage.setUpdateTime(System.currentTimeMillis() + "");
        boolean save = chatPageService.save(chatPage);

        return save ? R.ok("新增成功",chatPage.getId()) : R.error("新增失败");
    }

    @Operation(summary = "删除AI聊天页面")
    @DeleteMapping("/{id}")
    public R delChatPage(@PathVariable Integer id) {

        boolean update = chatPageService.lambdaUpdate()
                .eq(ChatPage::getId, id)
                .set(ChatPage::getStatus, 0)
                .update();
        return update ? R.ok("删除成功") : R.error("删除失败");
    }

    @Operation(summary = "修改ai聊天页面")
    @PutMapping("/")
    public R putChatPage(@RequestBody ChatPageDto chatPageDto) {

        boolean update = chatPageService.lambdaUpdate()
                .set(!chatPageDto.getPageName().isEmpty(), ChatPage::getPageName, chatPageDto.getPageName())

                .set(ChatPage::getUpdateBy, chatPageDto.getUpdateBy())
                .set(ChatPage::getUpdateTime, System.currentTimeMillis())
                .eq(ChatPage::getId, chatPageDto.getId())
                .update();

        return update ? R.ok("修改成功") : R.error("修改失败");
    }

    @Operation(summary = "根据userId获取AI聊天页面列表")
    @GetMapping("/{userId}")
    public R<List<ChatPageVo>> getChatPageList(@PathVariable("userId") long userId) {
        List<ChatPage> list = chatPageService.lambdaQuery()
                .eq(ChatPage::getUserId, userId)
                .eq(ChatPage::getStatus, 1)
                .list();

        List<ChatPageVo> list1 = new ArrayList<>();

        list.forEach(chatPage -> {
            ChatPageVo chatPageVo = new ChatPageVo();

            chatPageVo.setUserId(chatPage.getUserId());
            chatPageVo.setId(chatPage.getId());
            chatPageVo.setPageName(chatPage.getPageName());
            chatPageVo.setStatus(chatPage.getStatus());

            chatPageVo.setTime(DemoDate.getPostDate(chatPage.getUpdateTime()));
            list1.add(chatPageVo);
        });

        return R.ok("获取成功", list1);
    }

}
